<template>
  <van-icon
    :color="value ? '#ffa500' : '#777'"
    :name="value ? 'star' : 'star-o'"
    @click="clickCollect"
  />
</template>

<script>
import { addCollect, deleteCollect } from '@/api/user'
export default {
  name: 'article-collect',
  data () {
    return {
      loading: true
    }
  },
  props: {
    value: {
      type: Boolean,
      required: true
    },
    artId: {
      type: [Number, String, Object],
      required: true
    }
  },
  created () {

  },

  methods: {
    async clickCollect () {
      this.loading = true
      try {
        if (this.value) {
          // 已经收藏，取消收藏
          await deleteCollect(this.artId)
        } else {
          // 没有收藏，添加收藏
          await addCollect(this.artId)
        }
        // 更新视图
        this.$emit('input', !this.value)
        this.$toast.success(!this.value ? '收藏成功' : '取消收藏')
      } catch (err) {
        this.$toast.fail('操作失败，请稍候重试！')
      }
      this.loading = false
    }
  }
}
</script>

<style scoped lang='less'>

</style>
